import $api from "@/api/index.js"
import $i18n from "@/utils/i18n.js" // 多语言
export default {
	state: {
		authToken:'', // 认证token
		userInfo: '', // 用户信息
		parentUid:'', // 上级用户ID
		recList:[{
			label:$i18n.tc('my.task-wait-title'),
			name:"/static/images/icon-dtj.png",
			url:"my/my_receive?type=1",
			is_login:true
		},{
			label:$i18n.tc('my.task-audit-title'),
			name:"/static/images/icon-dsh.png",
			url:"my/my_receive?type=2",
			is_login:true
		},{
			label:$i18n.tc('my.task-success-title'),
			name:"/static/images/icon-ytg.png",
			url:"my/my_receive?type=4",
			is_login:true
		},{
			label:$i18n.tc('my.task-fail-title'),
			name:"/static/images/icon-wtg.png",
			url:"my/my_receive?type=3",
			is_login:true
		}],
		funlist:[{
			label:$i18n.tc('my.list-info-title'),
			name:"file-list-3-line",
			params:{
				'type':'page',
				'is_login':true,
				'url':"my/my_info"
			},
			show:true
		},{
			label:$i18n.tc('my.list-agent-title'),
			name:"file-list-3-line",
			params:{
				'type':'page',
				'is_login':true,
				'url':"my/my_agent"
			},
			show:true
		},{
			label:$i18n.tc('my.list-integral-title'),
			name:"exchange-dollar-line",
			params:{
				'type': 'page',
				'is_login' :true,
				'url': "accountlog/accountlog"
			},
			show:true
		},{
			label:$i18n.tc('my.list-collect-title'),
			name:"star-line",
			params:{
				'type':'page',
				'login':true,
				'url':"my/my_task_collect"
			},
			show:true
		},{
			label:$i18n.tc('my.list-feedback-title'),
			name:"feedback-line",
			params:{
				'type':'page',
				'login':true,
				'url':"workorder/workorder"
			},
			show:true
		},{
			label:$i18n.tc('my.list-notice-title'),
			name:"message-3-line",
			params:{
				'type':'page',
				'login':true,
				'url':"message/message"
			},
			show:true
		},{
			label:$i18n.tc('my.list-question-title'),
			name:"question-line",
			params:{
				'type':'page',
				'url':"article/article_list?id=",
			},
			show:true
		},{
			label:$i18n.tc('my.list-kefu-title'),
			name:"customer-service-2-line",
			params:{
				'type':'page',
				'url':"customer_service/customer_service"
			},
			show:true
		},{
			label:$i18n.tc('my.list-set-title'),
			name:"settings-line",
			params:{
				'type':'page',
				'login':true,
				'url':"setting/setting"
			},
			show:true
		}],
		messageTabs:[{
			name: $i18n.tc('message.tabs-1'),
		},{
			name: $i18n.tc('message.tabs-2'),
			type: 1
		},{
			name: $i18n.tc('message.tabs-3'),
			type: 2
		},{
			name: $i18n.tc('message.tabs-4'),
			type: 3
		}],
		messageList:{
			data: [],
			last_page: 1,
			current_page: 1
		},	
		messageParam:{
			page: 1,
			message_type:'all',
		},
	},
	mutations: {
		//更新内容
		async updateUserState(state, item) {
			let {
				key,
				val
			} = item
			state[key] = val
			if (['authToken', 'userInfo'].includes(key)) {
				uni.setStorageSync(key, val)
			}
		}
	},
	actions: {
		
		// 获取个人信息
		async getUserInfo({
			commit,
			state
		}, param) {
			if(!state.authToken)return;
			let res = await $api.user.getUserInfo()
			// console.log('更新用户信息')
			if(res.code == 200){
				commit('updateUserState', {
					key: 'userInfo',
					val: res.data
				})
			}
			// let data = res.code == 200 ? res.data : ''
		},
		
		// 获取通知消息
		async getUserMessage({
			commit,
			state
		}, param){
			let res = await $api.user.getUserMessage(param)
			
			let oldList = state.messageList;
			let newList = res.data;
			let list = {}
			if (param.page == 1) {
				list = newList;
			} else {
				newList.data = oldList.data.concat(newList.data) // 合并数组
				list = newList;
			}
			// console.log(list)
			commit('updateUserState', {
				key: 'messageList',
				val: list
			})
		},
		
	}
}
